package com.lzh.commonutils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lzhstart
 * @create 2021/4/21 9:03
 */
@Data
public class CommonResult {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    //私有的无参构造器
    private CommonResult(){}

    public static CommonResult ok() {
        CommonResult result = new CommonResult();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage("success");
        return result;
    }

    public static CommonResult error() {
        CommonResult result = new CommonResult();
        result.setSuccess(false);
        result.setCode(ResultCode.ERROR);
        result.setMessage("失败");
        return result;
    }

    public CommonResult success(Boolean success) {
        this.setSuccess(success);
        //this表示当前调用的对象
        return this;
    }
    public CommonResult code(Integer code) {
        this.setCode(code);
        return this;
    }

    public CommonResult data(String key,Object value) {
        this.data.put(key,value);
        return this;
    }
    public CommonResult data(Map<String,Object> map) {
        this.setData(map);
        return this;
    }
    public CommonResult message(String message) {
        this.setMessage(message);
        return this;
    }
}